
# include "dfxAudioDevice_OAL.h"
# include "dfxAudioSource_OAL.h"
# include "dfxLog.h"

namespace DFX
{
    AudioDevice_OAL::AudioDevice_OAL()
        : mDevice(NULL), mContext(NULL)
    {
    }

    AudioDevice_OAL::~AudioDevice_OAL()
    {
        Destroy();
    }

    DFXRESULT AudioDevice_OAL::Initialize()
    {
        mDevice = alcOpenDevice(NULL);

        if (!mDevice)
        {
            DFX_ERROR("Unable to create default audio device");
            return DFX_CREATEFAILED;
        }

        mContext = alcCreateContext(mDevice, NULL);

        if (!mContext)
        {
            DFX_ERROR("Unable to create audio context");
            return DFX_CREATEFAILED;
        }

        alcMakeContextCurrent(mContext);
        mInitialized = true;

        return DFX_SUCCESS;
    }

    DFXRESULT AudioDevice_OAL::Destroy()
    {
        if (mDevice)
        {
            alcDestroyContext(mContext);
            alcCloseDevice(mDevice);

            mContext = NULL;
            mDevice = NULL;
        }

        return DFX_SUCCESS;
    }

    AudioSource* AudioDevice_OAL::CreateAudioSource()
    {
        return new AudioSource_OAL();
    }
}